import { message } from 'antd';
import moment from 'moment';
import {
  socialgroupInformation,
  obtainDictionary,
  contributorList,
  nextSteps,
  submitInformation,
  insuranceInformationList,
  deleteinformation,
  modifyInformation,
  paymentGroup,
  providentFundGroup,
  providentFundMessage,
  submitProvidentFund,
  submitInsertAccuLs,
  providentFundInformationList,
  deleteProvidentInformation,
  modifyProvidentFund,
  modifyGroupName,
  modifyFunGroupName,
  getCooperaSuppList,
  getDeclarationPolicy,
  getCityListData,
  getPaymentPerListData,
  setDefaultValue,
  submitProMessageData,
  getCityNameData,
  deleteCombinaData,
  deleteAccuCombinaData,
  getInsListData,
  getAccuListData,
  getCooperaSupperData,
  changeUpdateEnableData,
  changeUpdateProhibitData,
  changeUpdateInsEnableData,
  changeUpdateInsProhibitData,
  getInsDetailListData,
  insReplacePolicy,
  accuReplacePolicy,
  getReliefPolicyList,
  insertSavePolicy,
  deleteSavePolicy,
  getLocRemission,
  updateSavePolicy,
  checkInsLsPkgName,
  checkSaveIns,
} from '@/services/more';

export default {
  state: {
    personnelcategory: {}, // 组合名称和区分缴费人员类别标识
    personnelcategoryData: [], // 获取缴费人员类别信息
    personnelinsuranceData: [], // 人员险种列表
    insuranceInformationData: [], // 当前险种所有信息
    dictionaryData: {}, // 字典值
    bizActiveKey: '',
    type: '',
    contributorData: [], // 缴费人员类别信息列表
    totaldataSource: [], // 总数据源
    payerIds: '', // 缴费人员类别id
    insuranceIds: '', // 险种id
    rangeDate: '', // 禁用开始和时间
    beginDate: '',
    ideIdsMessage: '', //
    addTime: [],
    switch: true, // 修改和保存切换
    disabledSwitch: false, // 输入框禁用
    groupId: '', // 组id
    current: 0,
    viewDataMessage: {}, // 查看右侧数据
    changeDataMessage: {}, // 查看右侧数据
    isModify: '', // 是不是修改的弹窗
    insLsPkgmessage: {}, // 组合信息
    providentFundPolicyId: '', // 政策组id
    personnelData: {},
    insureArr: [], // 五个险种
    clearProvidentFundId: '',
    provinceId: '', // 省 id
    areaNames: '北京市',
    tabid: '1',
    levelLength: '3',
    hshdRegFlag: null, // 是否区分缴费人员类别
    insLsPkgName: null, // 社保组合名称
    areaId: '752252', // 北京市小
    expandedKeys: ['752254'], // 北京大
    defaultareaIdArr: ['752252'], // 北京市小
    defaultareaId: ['752254'], // 北京大
    insDataSource: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    accuDataSource: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    pageData: { // 减免政策列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    assList: [], // 险种集合
    echoPolicyObj: {}, // 减免政策的回显数据
  },
  effects: {
    // 获取字典值
    *getDictCode({ payload, callback }, { call, put }) {
      const response = yield call(obtainDictionary, payload);
      if (response.success) {
        yield put({
          type: 'dictionaryMessage',
          payload: { response },
        });
        callback(response);
      } else {
        message.warning(response.message || '获取字典失败');
      }
    },

    // 新增政策组合前的校验
    *checkInsLsPkgName({ payload }, { call, put }) {
      const response = yield call(checkInsLsPkgName, payload);
      if (response.success) {
        yield put({
          type: 'nextSteps',
          payload: response,
        });
        yield put({
          type: 'updateModelData',
          payload: {
            hshdRegFlag: payload.hshdRegFlag,
            insLsPkgName: payload.insLsPkgName,
            areaId: payload.areaId,
          },
        });
      } else {
        message.warning(response.message || '服务端未知错误');
      }
    },

    // 新建政策组合
    *nextStep({ payload }, { call, put }) {
      const response = yield call(nextSteps, payload);
      if (response.success) {
        yield put({
          type: 'nextSteps',
          payload: response,
        });
      } else {
        message.warning(response.message || '服务端未知错误');
      }
      return response;
    },

    // 政策明细提交前的校验
    *checkSaveIns({ payload }, { call }) {
      const response = yield call(checkSaveIns, payload);
      return response;
    },

    // 获取减免政策列表数据
    *fetchReliefPolicy({ payload }, { call, put }) {
      const response = yield call(getReliefPolicyList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getReliefPolicyList',
          payload: {
            data: response.data && response.data || {},
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },

    // 减免政策提交
    *fetchSavePolicy({ payload }, { call }) {
      const response = yield call(insertSavePolicy, payload);
      if (response.success) {
        return response;
      } 
      return message.warning(response.message || '服务器端错误');
    },

    // 修改减免政策后的提交
    *fetchUpdatePolicy({ payload }, { call }) {
      const response = yield call(updateSavePolicy, payload);
      if (response.success) {
        return response;
      } 
      return message.warning(response.message || '服务器端错误');
    },

    // 删除减免政策
    *fetchDeletePolicy({ payload }, { call }) {
      const response = yield call(deleteSavePolicy, payload);
      if (response.success) {
        return response;
      } 
      return message.warning(response.message || '服务器端错误');
    },

    // 减免政策回显
    *getLocRemission({ payload }, { call, put }) {
      const response = yield call(getLocRemission, payload);
      yield put({
        type: 'updateModelData',
        payload: {
          echoPolicyObj: {},
        }
      })
      if (response.success) {
        yield put({
          type: 'updateModelData',
          payload: {
            echoPolicyObj: response.data || {},
          }
        })
      } else {
        message.warning(response.message || '服务器端错误');
      }
    },

    // 获取组合名称，缴费人员类别详情
    *socialFormation({ payload }, { put }) {
      // eslint-disable-next-line no-undef
      const response = yield call(socialgroupInformation, payload);
      if (response.success) {
        yield put({
          type: 'socialMessage',
          payload: {
            personnelcategory: response.data,
          },
        });
      } else {
        yield put({
          type: 'socialMessage',
          payload: {
            personnelcategory: {},
          },
        });
        message.warning(response.message || '查询失败');
      }
    },

    // 改变名字
    *changeGroupName({ payload, callback }, { call }) {
      const response = yield call(modifyGroupName, payload);
      callback(response);
    },
    *changeFunGroupName({ payload, callback }, { call }) {
      const response = yield call(modifyFunGroupName, payload);
      callback(response);
    },

    // 删除时间   后   禁用最顶部的时间
    *deletetimes({ payload }, { call, put, select }) {
      const response = yield call(deleteinformation, payload);
      if (response.success) {
        // yield put({
        //   type: 'disableTime',
        //   payload: { payload },
        // });
        message.success('删除成功！');
        const localpolicy = yield select(state => state.localpolicy);
        const paramet = {
          insLsPkgId: localpolicy.groupId,
          joinIdeId: localpolicy.payerIds,
          type: localpolicy.insuranceIds,
        };
        // 删除完以后获取该缴费人员下该险种下的所有信息
        yield put({
          type: 'localpolicy/insuranceInformation',
          payload: paramet,
        });
      } else {
        message.warning(response.message || '删除失败：服务端未知错误');
      }
    },

    // 公积金删除时间
    *deleteProvidentTimes({ payload, callback }, { call, put }) {
      const response = yield call(deleteProvidentInformation, payload);
      if (response.success) {
        yield put({
          type: 'disableTime',
          payload: { payload },
        });
        callback(response);
      } else {
        message.warning(response.message || '服务端未知错误');
      }
    },

    // 保存信息
    *saveInsurance({ payload, callback }, { call }) {
      // 2是新增
      // eslint-disable-next-line eqeqeq
      if (payload.isModify == 2) {
        const response = yield call(submitInformation, payload);
        if (response.success) {
          message.success(response.message || '保存成功');
          callback(response);
        } else {
          message.warning(response.message || '保存失败');
        }
      } else {
        const response = yield call(modifyInformation, payload);
        if (response.success) {
          message.success(response.message || '保存成功');
          callback(response);
        } else {
          message.warning(response.message || '保存失败');
        }
      }
    },

    // 保存信息
    *saveProvidentFund({ payload, callback }, { call, put }) {
      // 2是新增

      // eslint-disable-next-line eqeqeq
      if (payload.isModify == 2) {
        const response = yield call(submitProvidentFund, payload);
        if (response.success) {
          yield put({
            type: 'savetotalMessage',
            payload,
          });
          callback(response);
        } else {
          message.warning(response.message || '保存失败');
          callback(response);
        }
      } else {
        const response = yield call(modifyProvidentFund, payload);
        if (response.success) {
          callback(response);
        } else {
          message.warning(response.message || '保存失败');
          callback(response);
        }
      }
    },

    // 公积金有重复比例组合接口
    *submitInsertAccuLs({ payload }, { call }) {
      const response = yield call(submitInsertAccuLs, payload);
      return response;
    },

    // 获取缴费人员类别信息列表
    *contributorCategory({ payload, callback }, { call, put }) {
      const response = yield call(contributorList, payload);
      if (response.success) {
        yield put({
          type: 'contributorMessage',
          payload: {
            contributorData: response.data,
          },
        });
        callback(response.data);
      } else {
        yield put({
          type: 'contributorMessage',
          payload: {
            contributorData: {},
          },
        });
        message.warning(response.message || '获取缴费人员列表错误：服务端未知错误');
      }
    },

    // 该缴费人员下改险种下所有的信息
    *insuranceInformation({ payload }, { call, put }) {
      yield put({
        type: 'changeinsuranceMessage',
        payload: payload.type,
      });
      const response = yield call(insuranceInformationList, payload);
      if (response.success) {
        yield put({
          type: 'insuranceDataList',
          payload: response.data,
        });
      } else {
        yield put({
          type: 'insuranceDataList',
          payload: [],
        });
        message.warning(response.message || '查询政策信息失败');
      }
    },

    // 公积金下的所有信息
    *providentFundInformation({ payload, callback }, { call, put }) {
      const response = yield call(providentFundInformationList, payload);
      if (response.success) {
        yield put({
          type: 'insuranceDataList',
          payload: response.data,
        });
        callback(response);
      } else {
        yield put({
          type: 'insuranceDataList',
          payload: [],
        });
        message.warning(response.message || '查询政策信息失败');
      }
    },

    // 查看缴费组的id和是否区分缴费人员类别
    *paymentGroupId({ payload, callback }, { call, put }) {
      const response = yield call(paymentGroup, payload);
      if (response.success) {
        yield put({
          type: 'paymentGroupMessage',
          payload: {
            paymentGroupData: response.data,
          },
        });
        callback(response.data);
      } else {
        message.warning(response.message || '服务端未知错误');
      }
    },

    // 公积金组
    *combinationName({ payload, callback }, { call, put }) {
      const response = yield call(providentFundGroup, payload);
      if (response.success) {
        yield put({
          type: 'providentFundGroupId',
          payload: {
            providentFundId: response.data,
          },
        });
        callback(response.data);
      } else {
        message.warning(response.message || '服务端未知错误');
      }
    },

    // 公积金组的id
    *providentFundPolicy({ payload }, { call, put }) {
      const response = yield call(providentFundMessage, payload);
      if (response.success) {
        yield put({
          type: 'personnelMessage',
          payload: {
            personnelData: response.data,
          },
        });
      } else {
        yield put({
          type: 'personnelMessage',
          payload: {
            personnelData: {},
          },
        });
        message.warning(response.message || '查询失败');
      }
    },

    // 获取合作供应商
    *getCooperaSupplier({ payload }, { call }) {
      const response = yield call(getCooperaSuppList, payload);
      return response;
    },

    // 获取申报政策信息
    *declarationPolicy({ payload }, { call }) {
      const response = yield call(getDeclarationPolicy, payload);
      return response;
    },

    // 获取该城市下所有的地区
    // eslint-disable-next-line no-empty-pattern
    *getCityList({ payload }, { call }) {
      const response = yield call(getCityListData, payload);
      return response;
    },

    // 获取缴费人员类别
    *getPaymentPerList({ payload }, { call }) {
      const response = yield call(getPaymentPerListData, payload);
      return response;
    },

    // 设置社保组合默认值
    *setDefault({ payload }, { call }) {
      const response = yield call(setDefaultValue, payload);
      return response;
    },

    // 申报政策提交
    *submitProMessage({ payload }, { call }) {
      const response = yield call(submitProMessageData, payload);
      return response;
    },

    // 搜索地区
    *getCityName({ payload }, { call }) {
      const response = yield call(getCityNameData, payload);
      return response;
    },

    // 删除社保组合
    *deleteCombina({ payload }, { call }) {
      const response = yield call(deleteCombinaData, payload);
      return response;
    },

    // 删除公积金组合
    *deleteAccuCombina({ payload }, { call }) {
      const response = yield call(deleteAccuCombinaData, payload);
      return response;
    },

    // 获取社保组合列表
    *getInsList({ payload }, { call, put }) {
      // const response = yield call(getInsListData, payload);
      const {
        pagerArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getInsDataList',
        payload: {
          data: [],
          currentPage: 1,
          pageSize: 10,
        },
      });
      const response = yield call(getInsListData, payload);
      if (response.success) {
        yield put({
          type: 'getInsDataList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '服务端未知错误：获取社保组合列表失败');
      }
      // return response;
    },

    // 获取公积金组合列表
    *getAccuList({ payload }, { call, put }) {
      const {
        pagerArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getAccuDataList',
        payload: {
          data: [],
          currentPage: 1,
          pageSize: 10,
        },
      });
      const response = yield call(getAccuListData, payload);
      if (response.success) {
        yield put({
          type: 'getAccuDataList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '服务端未知错误：获取公积金组合列表失败');
      }
    },

    // 合作供应商详情
    *getCooperaSupper({ payload }, { call }) {
      const response = yield call(getCooperaSupperData, payload);
      return response;
    },

    // 获取树形数据
    *getTreeList({ payload }, { call }) {
      const response = yield call(getCityListData, payload);
      return response;
    },

    // 公积金 - 启用
    *changeUpdateEnable({ payload }, { call }) {
      const response = yield call(changeUpdateEnableData, payload);
      return response;
    },

    // 公积金 - 停用
    *changeUpdateProhibit({ payload }, { call }) {
      const response = yield call(changeUpdateProhibitData, payload);
      return response;
    },

    // 社保组合改变状态 - 启用
    *changeUpdateInsEnable({ payload }, { call }) {
      const response = yield call(changeUpdateInsEnableData, payload);
      return response;
    },

    // 社保组合改变状态 - 停用
    *changeUpdateInsProhibit({ payload }, { call }) {
      const response = yield call(changeUpdateInsProhibitData, payload);
      return response;
    },

    // 详情获取人员类别
    *getInsDetailList({ payload }, { call }) {
      const response = yield call(getInsDetailListData, payload);
      return response;
    },
    // 社保生成替数任务
    *insReplacePolicy({ payload }, { call }) {
      const response = yield call(insReplacePolicy, payload);
      return response;
    },
    // 公积金生成替数任务
    *accuReplacePolicy({ payload }, { call }) {
      const response = yield call(accuReplacePolicy, payload);
      return response;
    },
  },

  reducers: {
    // 新增
    // 获取字典值
    dictionaryMessage(state, { payload }) {
      return {
        ...state,
        dictionaryData: payload.response.data,
        insureArr: payload.response.data.BIZ_INS_TYPE,
      };
    },

    // 切换缴费人员类别
    changeMessage(state, { payload }) {
      return {
        ...state,
        payerIds: payload.value,
        insuranceIds: '3',
      };
    },

    // 切换险种
    changeinsuranceMessage(state, { payload }) {
      return {
        ...state,
        insuranceIds: payload,
      };
    },

    // 父组件传过来的缴费人员类别id
    mothideIdsvalue(state, { payload }) {
      return {
        ...state,
        ideIdsMessage: payload.ideIds.ideIds,
      };
    },

    // 获取缴费人员信息列表
    contributorMessage(state, { payload }) {
      const { BIZ_INS_TYPE } = state.dictionaryData;
      // eslint-disable-next-line array-callback-return
      payload.contributorData.map(item => {
        // eslint-disable-next-line array-callback-return
        BIZ_INS_TYPE.map(items => {
          // eslint-disable-next-line no-param-reassign
          items.insuranceTime = [];
        });
        // eslint-disable-next-line no-param-reassign
        item.itemInsurance = BIZ_INS_TYPE;
      });
      return {
        ...state,
        contributorData: payload.contributorData,
        payerIds: payload.contributorData[0] ? payload.contributorData[0].ideId : '',
        insuranceIds: BIZ_INS_TYPE[0] ? BIZ_INS_TYPE[0].itemCode : '',
        totaldataSource: payload.contributorData,
      };
    },

    // 获取组合名称和区分缴费人员类别标识
    socialMessage(state, { payload }) {
      return {
        ...state,
        personnelcategory: payload.personnelcategory,
      };
    },

    // 获取缴费人员类别信息
    personnelcategoryMessage(state, { payload }) {
      return {
        ...state,
        personnelcategoryData: payload.personnelcategoryData,
      };
    },

    // 点击下一步( 获取组的id )
    nextSteps(state, { payload }) {
      return {
        ...state,
        groupId: payload.data,
        current: 1, // 返回到第一个页签
        insuranceInformationData: [], // 中间的时间清空
        viewDataMessage: {}, // 查看右侧数据rangeDate: '',
        beginDate: '', // 禁用开始和时间
        rangeDate: '', // 禁用开始和时间
      };
    },

    // 获取减免政策列表数据
    getReliefPolicyList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },

    // 修改
    viewData(state, { payload }) {
      // eslint-disable-next-line array-callback-return
      state.insuranceInformationData.map(item => {
        // eslint-disable-next-line eqeqeq
        if (item.locStdId == payload.timeitem.locStdId) {
          // eslint-disable-next-line no-param-reassign
          item.active = true;
        } else {
          // eslint-disable-next-line no-param-reassign
          item.active = false;
        }
      });

      return {
        ...state,
        rangeDate: state.secondDate,
        viewDataMessage: payload.timeitem,
        changeDataMessage: payload.timeitem,
        isModify: '1',
      };
    },

    // 新增
    changeModify(state, { payload }) {
      return {
        ...state,
        rangeDate: state.beginDate,
        changeDataMessage: {},
        // viewDataMessage: {},
        isModify: payload,
        //
      };
    },

    // 查看缴费组的id和是否区分缴费人员类别
    providentFundGroupId(state, { payload }) {
      return {
        ...state,
        rangeDate: state.beginDate,
        changeDataMessage: {},
        isModify: 2,
        providentFundPolicyId: payload.providentFundId,
        clearProvidentFundId: payload.providentFundId,
        insuranceInformationData: [],
      };
    },
    // 清空组合名称
    // eslint-disable-next-line no-unused-vars
    changeProvidentFundModify(state, { payload }) {
      return {
        clearProvidentFundId: '',
      };
    },

    // 获取当前页信息
    insuranceDataList(state, { payload }) {
      let beginDate = '';
      let secondDate = '';
      let viewDataMessage = {};
      if (payload.length > 0) {
        beginDate = moment(payload[0].beginDate)
          .add(1, 'months')
          .format('YYYY-MM');
        // eslint-disable-next-line prefer-destructuring
        viewDataMessage = payload[0];
        // eslint-disable-next-line no-param-reassign
        payload[0].active = true;
        if (payload.length > 1) {
          secondDate = moment(payload[1].beginDate)
            .add(1, 'months')
            .format('YYYY-MM');
        }
      }
      return {
        ...state,
        beginDate,
        secondDate,
        viewDataMessage,
        insuranceInformationData: payload,
      };
    },

    // 缴费人员险种
    personnelFiveInsurance(state, { payload }) {
      return {
        ...state,
        personnelinsuranceData: payload.personnelinsuranceData,
        bizActiveKey: payload.personnelinsuranceData[0]
          ? payload.personnelinsuranceData[0].itemCode
          : '',
        type: payload.personnelinsuranceData.BIZ_INS_TYPE[0].itemCode,
      };
    },
    // 查看缴费组的id和是否区分缴费人员类别
    paymentGroupMessage(state, { payload }) {
      return {
        ...state,
        insLsPkgmessage: payload.paymentGroupData,
        groupId: payload.paymentGroupData.insLsPkgId,
        // paymentGroup: payload.paymentGroupData,
      };
    },

    personnelMessage(state, { payload }) {
      return {
        ...state,
        personnelData: payload.personnelData,
        providentFundPolicyId: payload.personnelData.accuLsPkgId,
      };
    },

    combinationmessage(state, { payload }) {
      return {
        ...state,
        tabid: payload,
      };
    },

    // 改变点击下一步的值
    changeCurrent(state, { payload }) {
      return {
        ...state,
        current: payload.current,
      };
    },
    // 保存信息
    preservationId(state, { payload }) {
      return {
        ...state,
        groupId: payload.insLsPkgId, // 组id
        payerIds: payload.joinIdeId, // 人id
        // current: payload.current,
      };
    },

    // 公积金清空对象和循环数组
    // eslint-disable-next-line no-unused-vars
    clearFunData(state, { _ }) {
      return {
        ...state,
        insuranceInformationData: [],
        viewDataMessage: {},
        providentFundPolicyId: '',
        rangeDate: '', // 禁用开始和时间
        beginDate: '',
      };
    },

    // 接受一个城市id，返回列表时定位
    cityId(state, { payload }) {
      return {
        ...state,
        areaId: payload,
      };
    },
    cityIds(state, { payload }) {
      return {
        ...state,
        areaNames: payload.cityName,
      };
    },
    provinceId(state, { payload }) {
      return {
        ...state,
        provinceId: payload.provinceId,
        areaId: payload.city,
      };
    },

    cityLength(state, { payload }) {
      return {
        ...state,
        levelLength: payload.length,
        // areaId: payload.city,
      };
    },

    // 改变左侧的城市
    leftCityids(state, { payload }) {
      const citys = [];
      citys.push(payload.city);
      return {
        ...state,
        defaultareaIdArr: citys,
        areaId: payload.city,
      };
    },
    // 改变左侧的城市
    leftCityid(state, { payload }) {
      const citys = [];
      citys.push(payload.city);
      return {
        ...state,
        defaultareaIdArr: payload.city,
        areaId: payload.city[0],
      };
    },
    // 改变左侧的城市时的父城市的id
    firstStageCity(state, { payload }) {
      const citys = [];
      citys.push(payload.city[1]);
      return {
        ...state,
        defaultareaId: citys,
        expandedKeys: citys,
      };
    },

    // 改变左侧的城市时的父城市的id
    firstStageCitys(state, { payload }) {
      const citys = [];
      citys.push(payload.city);
      return {
        ...state,
        defaultareaId: citys,
        expandedKeys: citys,
      };
    },

    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    // 获取社保组合列表
    getInsDataList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        insDataSource: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },

    // 获取社保组合列表
    getAccuDataList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        accuDataSource: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
  },
};
